<?php

class TicketController extends Controller {
    /**
     * @var string the default layout for the views. Defaults to 'column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout='application.views.layouts.column1';

    /**
     * @var CActiveRecord the currently loaded data model instance.
     */
    private $_model;

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionIndex() {
        $model=new Ticket('captcha');

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Ticket'])) {
            $model->attributes=$_POST['Ticket'];

            if($model->save())
                Yii::app()->user->setFlash('success',"Ваше сообщение успешно отправленно");
            else {
                Yii::app()->user->setFlash('success',"Сообщение не отправленно");
                return $this->render('create',array(
                        'model'=>$model,
                ));
            }
            $this->redirect(array('message'));

        }

        $this->render('create',array(
                'model'=>$model,
        ));
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     */


    /**
     * Lists all models.
     */
    public function actionMessage() {
        $this->render('index');
    }



    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     */
    public function loadModel() {
        if($this->_model===null) {
            if(isset($_GET['id']))
                $this->_model=Ticket::model()->findbyPk($_GET['id']);
            if($this->_model===null)
                throw new CHttpException(404,'The requested page does not exist.');
        }
        return $this->_model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model) {
        if(isset($_POST['ajax']) && $_POST['ajax']==='ticket-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
